package fileProcessing.PreprocessingAnalysis;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import audit.AuditWriter;

import userInterface.GUIController;

import fileProcessing.ParsingException;
import fileProcessing.preprocessorException;

public class FileAnalysis {
	private String path;
	private Scanner inFile;
	private String[] files;
	protected int[] fileStructTypes;
	File temp;
	private String currentFile;
	private ArrayList<Integer> headerLine = new ArrayList<Integer>();
	private ArrayList<String> headerFields = new ArrayList<String>();
	public static AuditWriter audit;
	
	/**
	 * Constructor for processing each subfile
	 * @param dirPath Path to subfiles
	 * @throws preprocessorException 
	 * @throws ParsingException 
	 */
	public FileAnalysis(String dirPath) throws preprocessorException, ParsingException{
		path = dirPath;
		temp = new File(dirPath);
		audit = AuditWriter.getAuditWriter();
		try {
			examineFiles();
		} catch (FileNotFoundException e) {
			audit.writeToAudit("unable to analyze files.");
		}
	}
	
	public ArrayList<File> getPreprocessingFiles(){
		ArrayList<File> processedFiles = new ArrayList<File>(files.length);
		for(String file : files){
			processedFiles.add(new File(path+"\\"+file));
		}
		return processedFiles;
	}
	
	public ArrayList<Integer> getHeaderLineResults(){
		return headerLine;
	}
	public ArrayList<String> getHeaderLineMatches(){
		return headerFields;
	}
	/**
	 * Determine how files are structured
	 * @throws FileNotFoundException
	 * @throws preprocessorException 
	 * @throws ParsingException 
	 */
	public void examineFiles() throws FileNotFoundException, preprocessorException, ParsingException{
		String tempFileName;
		int fileCounter=1;
		FindHeaderLine newHeaderSearch;
		files = temp.list();
		sortFileNames();
		for(String s : files){
			tempFileName = path+s;
			currentFile = tempFileName;
			inFile = new Scanner(new FileReader(tempFileName));
			audit.writeToAudit("Preprocessing of source #" + fileCounter + "\n");
			GUIController.getMainPage().setStatus("Preprocessing of source #" + fileCounter + "\n");
			GUIController.getMainPage().task.notify(1f/files.length*45);
			//System.out.println("Preprocessing of source #"+fileCounter);
			newHeaderSearch = new FindHeaderLine(tempFileName);
			headerLine.add(newHeaderSearch.headerLine);
			headerFields.add(newHeaderSearch.headerLineString);
			fileCounter++;
		}
	}
	
	public void sortFileNames(){
		int numberOfFiles = files.length,fileNumber=1;
		String[] sortedList = new String[numberOfFiles];
		Pattern fileNamePattern = Pattern.compile("(\\w+)(\\d+)(\\..+)",Pattern.CASE_INSENSITIVE);
		Matcher fileNameMatcher = fileNamePattern.matcher(files[0]);
		fileNameMatcher.find();
		String fileRootName = fileNameMatcher.group(1), fileExtension = fileNameMatcher.group(3);
		for(String s : sortedList){
			s = fileRootName+Integer.toString(fileNumber)+fileExtension;
			files[fileNumber-1] = s;
			fileNumber++;
		}
	}	
}
